<template>
	<div class="bg-white">
		<Form :label-width="80">
			<FormItem class="w-100" label="部门名称">
				<Input v-bind:readonly="true" v-model="dept.name"></Input>
			</FormItem>
			<FormItem class="w-100" label="职位名称">
				<Input v-model="job.name"></Input>
			</FormItem>
			<FormItem class="w-100" label="父级职位">
				<Select v-model="pJob.jobId">
				  <Option v-for="job in pJobs" :value="job.jobId" :key="job.jobId">{{ job.name }}</Option>
				</Select>
			</FormItem>
			<FormItem class="text-right">
				<Button @click="submit" type="primary">提交</Button>
			</FormItem>
		</Form>
	</div>
</template>

<script>
	export default {
		props: {
			params: {
				type: Object,
				default: undefined,
			}
		},
		data() {
			return {
				dept: {
					deptId: "",
					name: "",
					empId: "",
				},
				job: {
					jobId: "",
					name: "",
					pJobId: "",
				},
				pJob: {
					jobId: "",
					name: "",
					pJobId: "",
				},
				pJobs: [],
			}
		},
		methods: {
			init: function() {
				this.search();
				this.searchPJob();
			},
			search: function() {
				this.http.get({
					url: "/dept/job/all",
					param: {
						deptId: this.dept.deptId
					}
				}).then(data => {
					this.pJobs = data;
				});
			},

			searchPJob: function() {
				this.http.get({
					url: "/dept/job/info",
					param: {
						jobId: this.job.pJobId,
					}
				}).then(data => {
					this.pJob = data;
				});
			},

			submit: function() {
				if ("" === this.job.jobName) {
					this.message.error('职位名称不能为空')
				} else {
					this.http.put({
						url: "/dept/job/info",
						param: {
							jobId: this.job.jobId,
							deptId: this.dept.deptId,
							name: this.job.name,
							pJobId: this.pJob.jobId,
						},
					}).then(() => {
						this.message.info("修改成功");
						this.$router.detail.close();
					});
				}
			},
		},
		mounted: function() {
			this.dept.deptId = this.params.deptId;
			this.dept.name = this.params.deptName;
			this.job.name = this.params.jobName;
			this.job.jobId = this.params.jobId;
			this.job.pJobId = this.params.pJobId;
			this.init();
		}
	}
</script>

<style>
</style>
